require.config({
    paths: {
        "common": "common/common",
        "pagin": "common/pagin",
        "dept": "data/deptData"
    }
    // baseUrl: 'js/common'
});

require(['common', 'dept', 'pagin'], function(common, D, pagin) {
    var baseUrl = common.defaultStr();
    /*----------
     *ajax数据请求
     *部门页面管理————补助页面
     *部门页面管理————补发页面
     *默认打开该页的时候就已经知道了是那个部门
     *并且只显示该部门的信息
     */

    var user_table = new Vue({
        el: '#main-data',
        data: {
            items: [],
            query: {
                "rows": 10,
                "page": 1
            },
            username: '',
            check_deptId: [],
            delItem: '',
            editItem: '',
            cur_page: 1,
            roles: [{
                id: 0,
                name: "管理员"
            }, {
                id: 1,
                name: "老师"
            }, {
                id: 2,
                name: "学生"
            }],
            depts: [],
            pages: [],
            newItem: '',
            check_AllDept: false,
            url: {
                'queryUser': baseUrl + '/base/user/list.do',
                'addUser': baseUrl + '/base/user/add.do',
                'delUser': baseUrl + '/base/user/delete.do',
                'getDepts': baseUrl + '/base/dept/list.do'
            }
        },
        methods: {
            /*初始化页面*/
            init: function() {
                var _self = this;
                _self.queryData();
                _self.queryDepts();
            },
            /*用户提交查询信息，查询用户信息*/
            queryData: function(str) {
                var _self = this;
                /*提交数据到服务器 query是要查询的数据对象*/
                if (str == "page1") {
                    _self.query = [];
                    _self.query = {
                        "rows": 10,
                        "page": 1,
                        "deptId": ''
                    };
                    _self.curPage = 1;
                }
                if (str == "all") {
                    _self.query.page = 1;
                    _self.curPage = 1;
                }
                _self.query.loginName = _self.username;
                _self.query.deptId = common.arrayToStr(_self.check_deptId);
                $.ajax({
                    type: "POST",
                    url: _self.url.queryUser,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(_self.query),
                    dataType: "json",
                    success: function(result) {
                        _self.items = [];
                        console.log(result);
                        $.each(result.result, function(i, val) {
                            _self.items.push(val)
                        });
                        /* 设置分页*/
                        if (result.total > _self.query.rows) {
                            console.log("fenye")
                                /*如果总数大于10就分页*/
                                /*_self.totalPage 总页数*/
                            var totalPage = Math.ceil(result.total / _self.query.rows);
                            var page_result = pagin.paginGetli(totalPage, _self.curPage);
                            _self.pages = page_result.pages;
                        } else {
                            _self.pages = [];
                        }
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                _self.query.page = cur_page;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理分页查询*/
            submitPage: function(p, b) {
                var _self = this;
                console.log(p);
                if (b) {
                    return false;
                }
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            /*查询数据*/
            queryDepts: function() {
                var _self = this;
                var data = {
                    "rows": 200,
                    "page": 1,
                };
                D.queryDepts(data).done(function(result) {
                    _self.depts = [];
                    $.each(result.result, function(i, value) {
                        _self.depts.push(value);
                    })
                })
            },
            delUser: function(id, loginName, deptId) {
                var _self = this;
                _self.delItem = {
                    "id": id,
                    "name": loginName,
                    "deptId": deptId
                }
            },
            confirmDel: function() {
                var _self = this;
                var data = {
                    "loginName": this.delItem.name
                };
                $.ajax({
                    type: "POST",
                    url: _self.url.delUser,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    dataType: "json",
                    success: function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-del').modal('hide');
                            location.reload();

                            // _self.queryData('page1');
                        }, 1000);
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*编辑用户角色*/
            editUser: function(id, loginName, deptId) {
                var _self = this;
                _self.editItem = {
                    "id": id,
                    "name": loginName,
                    "deptId": deptId
                }
            },
            confirmEdit: function() {
                var _self = this;

            },
            addUser: function() {
                var _self = this;
                // _self.url.addUser.
                $.ajax({
                    type: "POST",
                    url: _self.url.addUser,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(_self.newItem),
                    dataType: "json",
                    success: function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-add').modal('hide');
                            _self.queryData('page1');
                        }, 1000);
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            checkAllDept: function() {
                var _self = this;
                console.log('check_Allmonth');
                _self.check_deptId = [];
                if (!_self.check_AllDept) {
                    $.each(_self.depts, function(i, value) {
                        _self.check_deptId.push(value.deptId);
                    })
                };
                // _self.queryData()

            }
        },
        watch: {
            check_deptId: function(val) {
                var that = this;
                if (val.length < this.depts.length) {
                    that.check_AllDept = false;
                } else {
                    that.check_AllDept = true;
                }
                this.queryData('all');
            },
            username: function() {
                this.queryData('all');
            }
        }
    });
    user_table.init();
});